package com.lagou.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author yutao
 * @create 2020-06-21 16:48
 **/
@RestController
public class TestController {


    @RequestMapping("getName")
    public String getName() throws SQLException {
        DruidDataSource instance = DBUtil.getInstance();
        DruidPooledConnection connection = instance.getConnection();
        String sql = "select username from user limit 1";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        String result = null;
        while (resultSet.next()) {
            result = resultSet.getString("username");
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return result;

    }

}
